<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h3>队列</h3>
    <script>
        //对队列结构进行一个封装
        class Queue {
            constructor() {
                    this.item = [];
                }
                //插入数据
            enqueue(element) {
                    this.item.push(element);
                }
                //删除
            dequeue() {
                return this.item.shift();
            }
            peek() {
                if (this.isEmpty()) {
                    return undefined;
                }
                return this.item[0];
            }

            isEmpty() {
                return this.item.length === 0
            }

            size() {
                return
            }
        }



        // queue.enqueue("china");
        // queue.enqueue("korea");
        // queue.enqueue("japan");
        // console.log("插入测试", queue.item);
        // queue.dequeue();
        // console.log("删除测试", queue.item);
        // console.log("第一个元素", queue.peek());
        // console.log("是否为空", queue.isEmpty());
        // console.log("元素数量", queue.size());

        //应用，击鼓传花

        //组织人员玩游戏

        function playGame(player, num) {
            //应用队列
            const queue = new Queue();
            //将游戏玩家储存到队列当中
            for (let i = 0; i < player.length; i++) {
                queue.enqueue(player[i]);
            }
            console.log("参与游戏的玩家", queue.toString());
            //循环游戏
            while (queue.size() > 1) {
                for (let i = 0; i < num - 1; i++) {
                    queue.enqueue(queue.dequeue());
                }
                outedPlayer = queue.dequeue();
                console.log("被淘汰玩家：", outedPlayer);
            }
            console.log("获胜玩家", queue);
        }
        const player = ["a", "b", "c", "d", "e", "f"];
        const num = 3;
        playGame(player)
    </script>
</body>

</html>